module GroupsThreadsHelper
  
	def new_posts?(thread)	
		if current_member == nil
			return false
		end
		
		last_visit = current_member.thread_visits.where(:thread_id => thread.id).first
		if last_visit == nil
			return true
		else
			return thread.updated_at > last_visit.updated_at
		end
	end

	def group_new_posts?(group)
		if current_member == nil
			return false
		end

 		join_query = 'LEFT JOIN thread_visits ON groups_threads.id = thread_visits.thread_id'
		where_query = " thread_visits.member_id = ? AND (thread_visits.updated_at IS NULL OR thread_visits.updated_at < groups_threads.updated_at)"
		number_of_new_threads = group.groups_threads.joins(join_query).where(where_query,current_member.id).count

    return (number_of_new_threads > 0)
	end




	def group_permission?(group)

		if current_member
			if group.permission.id >= current_member.permission.id
			return true
			end
		else
			if group.permission.id >= 5
			return true
			end
		end

		return false

	end

end
